/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "connectproxy11_fuzzer.h"
#include "fuzzer/FuzzedDataProvider.h"
#include "multimodal_input_connect_manager.h"
#include "multimodal_input_connect_proxy.h"

#include "securec.h"

#undef MMI_LOG_TAG
#define MMI_LOG_TAG "ConnectProxy11FuzzTest"

namespace OHOS {
namespace MMI {
void GetShieldStatusFuzz(FuzzedDataProvider &fdp)
{
    int32_t shieldMode = fdp.ConsumeIntegral<int32_t>();
    bool isShield = fdp.ConsumeBool();
    MULTIMODAL_INPUT_CONNECT_MGR->GetShieldStatus(shieldMode, isShield);
}

void GetKeyStateFuzz(FuzzedDataProvider &fdp)
{
    std::vector<int32_t> pressedKeys = {
        fdp.ConsumeIntegral<int32_t>(),
        fdp.ConsumeIntegral<int32_t>(),
        fdp.ConsumeIntegral<int32_t>(),
        fdp.ConsumeIntegral<int32_t>()
    };
    int32_t key = fdp.ConsumeIntegral<int32_t>();
    int32_t value = fdp.ConsumeIntegral<int32_t>();
    std::map<int32_t, int32_t> keyStates;
    keyStates.insert(std::make_pair(key, value));

    key = fdp.ConsumeIntegral<int32_t>();
    value = fdp.ConsumeIntegral<int32_t>();
    keyStates.insert(std::make_pair(key, value));

    key = fdp.ConsumeIntegral<int32_t>();
    value = fdp.ConsumeIntegral<int32_t>();
    keyStates.insert(std::make_pair(key, value));
    MULTIMODAL_INPUT_CONNECT_MGR->GetKeyState(pressedKeys, keyStates);
}

void AuthorizeFuzz(FuzzedDataProvider &fdp)
{
    bool isAuthorized = fdp.ConsumeBool();
    MULTIMODAL_INPUT_CONNECT_MGR->Authorize(isAuthorized);
}

void RequestInjectionFuzz(FuzzedDataProvider &fdp)
{
    int32_t status = fdp.ConsumeIntegral<int32_t>();
    int32_t reqId = fdp.ConsumeIntegral<int32_t>();
    MULTIMODAL_INPUT_CONNECT_MGR->RequestInjection(status, reqId);
}

void QueryAuthorizedStatusFuzz(FuzzedDataProvider &fdp)
{
    int32_t status = fdp.ConsumeIntegral<int32_t>();
    MULTIMODAL_INPUT_CONNECT_MGR->QueryAuthorizedStatus(status);
}

void ConnectProxy11FuzzTest(const uint8_t *data, size_t size)
{
    FuzzedDataProvider fdp(data, size);
    GetShieldStatusFuzz(fdp);
    GetKeyStateFuzz(fdp);
    AuthorizeFuzz(fdp);
    RequestInjectionFuzz(fdp);
    QueryAuthorizedStatusFuzz(fdp);
}
} // MMI
} // OHOS

/* Fuzzer entry point */
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{
    /* Run your code on data */
    if (data == nullptr) {
        return 0;
    }

    OHOS::MMI::ConnectProxy11FuzzTest(data, size);
    return 0;
}
